package Offer.offer2016;

public class Test11 {

	public static void main(String[] args) 
	{
		System.out.println(power(-2,-2));
		System.out.println(Power.power(-2,-2));
	}
    public static double power(double base,int exponent)
    {
    	boolean inValidInput=false;
    	if(equal(base,0.0)&&exponent<0)
    	{
    		inValidInput=false;
    		return 0.0;
    	}
    	int absExponent=exponent;
    	if(exponent<0)
    	{
    		absExponent=-exponent;
    	}
    	double result=1.0;
    	for(int i=1;i<=absExponent;i++)
    	{
    		result *=base;
    	}
    	if(exponent<0)
    	{
    		result=1.0/result;
    	}
    	return result;
    }
    public static boolean equal(double base,double num)
    {   	
    	if(base-num<0.00000001&&base-num>-0.00000001)
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    		
    }
}

class Power{
	public static double power(double base, int exponent){
		if(equals(base,0) && exponent < 0){
			throw new RuntimeException("��ĸ����Ϊ��");
		}
		int exp = exponent;
		if(exponent < 0){
			exp = -exponent;
		}
		double result = powerWithUnsignedExponent(base, exp);
		if(exponent < 0){
			result = 1/result;
		}
		return result;
	}
	public static double powerWithUnsignedExponent(double base, int exponent){
		if(exponent == 0){
			return 1;
		}
		if(exponent == 1){
			return base;
		}
		double result = powerWithUnsignedExponent(base, exponent>>1);
		result *= result;
		if ((exponent & 1) == 1) {
			result *= base;
		}
		return result;
	}
	
	public static boolean equals(double num1,double num2) {
		if(Math.abs(num1 - num2) <= 0.0000001){
			return true;
		}
		return false;
	}
}
